//package com.example.financechain.Utils;
//
//
//import com.example.financechain.domain.BlockInfomation;
//import com.example.financechain.domain.entity.Role;
//import org.apache.commons.codec.binary.Hex;
//import org.hyperledger.fabric.gateway.*;
//import org.hyperledger.fabric.sdk.BlockInfo;
//import org.hyperledger.fabric.sdk.Channel;
//import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
//import org.hyperledger.fabric.sdk.exception.ProposalException;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.InputStreamReader;
//import java.io.Reader;
//import java.nio.charset.StandardCharsets;
//import java.security.InvalidKeyException;
//import java.security.PrivateKey;
//import java.security.cert.CertificateException;
//import java.security.cert.X509Certificate;
//import java.util.ArrayList;
//import java.util.List;
//
////用组织1的证书去查询区块
//
///**
// * 根据区块号查询hash
// */
//@Component
//public class QueryBlockHeightAndLatestBlock {
//
//    public BlockInfomation queryBlockinfo(int number) throws IOException, CertificateException, InvalidKeyException {
//
//
//        BlockInfomation blockInfomation2 = new BlockInfomation();
//        Role role = UserThreadLocal.get();
//        // Connect to the network using the gateway
//        try (Gateway gateway = GatewayUtilss.build(role).connect()) {
//            Network network = gateway.getNetwork("energycarchannel");
//
//            // Get the channel and query the blockchain for the block count and latest block
//            Channel channel = network.getChannel();
//
//            long blockCount = channel.queryBlockchainInfo().getHeight();
//
//            BlockInfo blockInfo = channel.queryBlockByNumber(number);
//
//
//            Iterable<BlockInfo.EnvelopeInfo> envelopeInfos = blockInfo.getEnvelopeInfos();
//            String txid ;
//            for (BlockInfo.EnvelopeInfo envelopeInfo : envelopeInfos) {
//               //资源消耗太大，后期关掉
//                System.out.println("Transaction ID: " + envelopeInfo.getTransactionID());
//                txid = envelopeInfo.getTransactionID();
//            }
//
//            blockInfomation2.setNumber(blockInfo.getBlockNumber());
//            blockInfomation2.setPrehash(Hex.encodeHexString(blockInfo.getPreviousHash()));
//            blockInfomation2.setHash(Hex.encodeHexString(blockInfo.getDataHash()));
//
//
//        } catch (Exception e) {
//            System.err.println("Failed to query blocks: " + e);
//
//
//        }
//    return blockInfomation2;
//
//    }
//
//    /**
//     * 查询高度
//     * @return
//     */
//    public   Long  queryheight(){
//        Long lasteheight = null;
//        Role role = UserThreadLocal.get();
//        try (Gateway gateway = GatewayUtilss.build(role).connect()) {
//            Network network = gateway.getNetwork("energycarchannel");
//
//            Channel channel = network.getChannel();
//
//            lasteheight= channel.queryBlockchainInfo().getHeight();
//
//    } catch (CertificateException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (InvalidKeyException e) {
//            e.printStackTrace();
//        } catch (InvalidArgumentException e) {
//            e.printStackTrace();
//        } catch (ProposalException e) {
//            e.printStackTrace();
//        }
//
//        return lasteheight;
//    }
//
//
//    public  List<String> querDetail(int number){
//
//        List<String> list = new ArrayList<>();
//        BlockInfomation blockInfomation2 = new BlockInfomation();
//        // Connect to the network using the gateway
//        Role role = UserThreadLocal.get();
//        try (Gateway gateway = GatewayUtilss.build(role).connect()) {
//            Network network = gateway.getNetwork("energycarchannel");
//
//            // Get the channel and query the blockchain for the block count and latest block
//            Channel channel = network.getChannel();
//
//            long blockCount = channel.queryBlockchainInfo().getHeight();
//
//            BlockInfo blockInfo = channel.queryBlockByNumber(number);
//
//
//            Iterable<BlockInfo.EnvelopeInfo> envelopeInfos = blockInfo.getEnvelopeInfos();
//            String txid = null;
//            for (BlockInfo.EnvelopeInfo envelopeInfo : envelopeInfos) {
//                System.out.println("Transaction ID: " + envelopeInfo.getTransactionID());
//                txid = envelopeInfo.getTransactionID();
//            }
//
//            if(txid==null){
//                return null;
//            }
//            list = ResultCode.queryFromBlockByTxId(txid,channel);
//
//
//        } catch (Exception e) {
//            System.err.println("Failed to query blocks: " + e);
//
//
//        }
//        return list;
//    }
//
//
//    public  List<String> querDetailbyTXID(String txid){
//
//        List<String> list = new ArrayList<>();
//        BlockInfomation blockInfomation2 = new BlockInfomation();
//        // Connect to the network using the gateway
//        Role role = UserThreadLocal.get();
//        try (Gateway gateway = GatewayUtilss.build(role).connect()) {
//            Network network = gateway.getNetwork("energycarchannel");
//
//            // Get the channel and query the blockchain for the block count and latest block
//            Channel channel = network.getChannel();
//
//            long blockCount = channel.queryBlockchainInfo().getHeight();
//
//            //BlockInfo blockInfo = channel.queryBlockByNumber(number);
//
//
//
//            list = ResultCode.queryFromBlockByTxId(txid,channel);
//
//
//        } catch (Exception e) {
//            System.err.println("Failed to query blocks: " + e);
//
//
//        }
//        return list;
//    }
//
//
//
//    private static X509Certificate readX509Certificate(final InputStream inputStream) throws IOException, CertificateException {
//
//        try (Reader certificateReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);) {
//            return Identities.readX509Certificate(certificateReader);
//        }
//    }
//
//    private static PrivateKey getPrivateKey(final InputStream inputStream) throws IOException, InvalidKeyException {
//        try (Reader privateKeyReader = new InputStreamReader(inputStream,StandardCharsets.UTF_8)) {
//            return Identities.readPrivateKey(privateKeyReader);
//        }
//    }
//}
